// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <ktest/ktest.h>

#include <nebula/testing/ktest_util.h>
#include <nebula/util/time.h>

namespace nebula {
namespace util {

TEST(TimeTest, ConvertTimestampValue) {
  auto convert = [](TimeUnit::type in, TimeUnit::type out, int64_t value) {
    return ConvertTimestampValue(timestamp(in), timestamp(out), value).value_or_die();
  };

  auto units = {
      TimeUnit::SECOND,
      TimeUnit::MILLI,
      TimeUnit::MICRO,
      TimeUnit::NANO,
  };

  // Test for identity
  for (auto unit : units) {
    EXPECT_EQ(convert(unit, unit, 0), 0);
    EXPECT_EQ(convert(unit, unit, INT64_MAX), INT64_MAX);
    EXPECT_EQ(convert(unit, unit, INT64_MIN), INT64_MIN);
  }

  EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::MILLI, 2), 2000);
  EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::MICRO, 2), 2000000);
  EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::NANO, 2), 2000000000);

  EXPECT_EQ(convert(TimeUnit::MILLI, TimeUnit::SECOND, 7000), 7);
  EXPECT_EQ(convert(TimeUnit::MILLI, TimeUnit::MICRO, 7), 7000);
  EXPECT_EQ(convert(TimeUnit::MILLI, TimeUnit::NANO, 7), 7000000);

  EXPECT_EQ(convert(TimeUnit::MICRO, TimeUnit::SECOND, 4000000), 4);
  EXPECT_EQ(convert(TimeUnit::MICRO, TimeUnit::MILLI, 4000), 4);
  EXPECT_EQ(convert(TimeUnit::MICRO, TimeUnit::SECOND, 4000000), 4);

  EXPECT_EQ(convert(TimeUnit::NANO, TimeUnit::SECOND, 6000000000), 6);
  EXPECT_EQ(convert(TimeUnit::NANO, TimeUnit::MILLI, 6000000), 6);
  EXPECT_EQ(convert(TimeUnit::NANO, TimeUnit::MICRO, 6000), 6);
}

}  // namespace util
}  // namespace nebula
